home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / misc / sci / RARS_Amiga_3.lha / RARS / track.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-27  |  6.1 KB  |  215 lines

  1. // TRACK.CPP - Race Course definition
  2. // by Mitchell E. Timin, State College, PA
  3. // see CAR.H & TRACK.H for class and structure declarations
  4. // This version is for Borland C++, version 3.1, and is for DOS
  5. // This is part of version 0.60 of RARS (Robot Auto Racing Simulation)
  6. // ver. 0.1 release January 12, 1995
  7. // ver. 0.2 1/23/95
  8. // ver. 0.3 2/7/95
  9. // ver. 0.39 3/6/95
  10. // March 9 - build_track() changed to automatically handle degrees or radians
  11. // ver. 0.46 3/25/95
  12. // ver. 0.6b 5/8/95 b for beta
  13.  
  14. /* (12.5.95) Added support for tracks that describe the centerline of the track instead of the
  15.  *           outer rail. This is triggered by specifying a negative number of segments.
  16.  * (21.5.95) Made minor modifications which should work better on other platforms. Some of the
  17.  *           8.3 filename handling code has been removed.
  18.  *           Marcel Offermans
  19.  */
  20.  
  21. #include <fstream.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "track.h"
  25. #include "gi.h"         // needed only for resume_text_display()
  26.  
  27. // array for track data file name:
  28. char trackfile[60];
  29.  
  30. int NSEG;  // number of track segments (see drawpath() in GRAPHICS.CPP)
  31. double X_MAX, Y_MAX; // maximum values that display must show, feet
  32. double width;       // width of track, feet
  33. double TRK_STRT_X; // coordinates of where to start drawing track
  34. double TRK_STRT_Y;  // (feet)
  35. double SCORE_BOARD_X;  // These are in feet, track coordinates
  36. double SCORE_BOARD_Y; // (where the scoreboard is located)
  37. double LDR_BRD_X;      // upper left corner of leader board, feet
  38. double LDR_BRD_Y;
  39. double LOTIX, LOTIY;   // coords of start of "Length of track is ......"
  40. double IP_X, IP_Y;     // Instrument Panel coordinates
  41. double FINISH;         // fraction of segment 0 prior to finish line
  42. double from_start_to_seg1;  // distance from start/finish line to end of segment
  43.  
  44. // These two arrays describe the outer and inner boundaries of the track:
  45. segment *trackout;
  46. segment *trackin; // Note that the only difference between trackin and
  47.                   // trackout is a "width" feet smaller radius on the curves.
  48.  
  49. void err_exit(void)
  50. {
  51.      resume_normal_display();
  52.      cout << "There is no track data file.  :(" << endl;
  53.      if(trackfile[0])
  54.         cout << "Tried to find: " << trackfile << endl;
  55.      exit(1);
  56. }
  57.  
  58. // Reads the track definition file and builds the trackin and trackout
  59. // arrays.  If the arc length of the first curve is < 5.0 the units are
  60. // assumed to be radians.  Otherwise they are assumed to be degrees.
  61. void build_track()
  62. {
  63.    ifstream inf(trackfile); // open the track file for input
  64.    static int degrees = -1; // will become 1 for degrees, 0 for radians
  65.    int i;
  66.  
  67.     /* flag indicating if this track is described by its centerline */
  68.     static int centerline = 0;
  69.     /* used in centerline parsing */
  70.     double radius, length, halfwidth;
  71.  
  72.     /* if the trackfile couldn't be found, try appending .trk to the filename */
  73.     if(!inf)
  74.     {
  75.         strcat(trackfile, ".trk");
  76.         inf.open(trackfile);
  77.         /* if that doesn't work either, we fail */
  78.         if(!inf)
  79.         {
  80.             err_exit();
  81.         }
  82.     }
  83.     else
  84.     {
  85.         /* check if the file had the .trk extension */
  86.         if(strcmpnocase(&trackfile[(strlen(trackfile) - 4)], ".trk"))
  87.         {
  88.             resume_normal_display();
  89.             cout << "Track data file should have a .trk extension." << endl;
  90.             exit(1);
  91.         }
  92.     }
  93.     inf >> NSEG;
  94.  
  95.     /* check for new type */
  96.     if (NSEG < 0)
  97.     {
  98.         /* this is a new type of track, described by the centerline of the circuit */
  99.         centerline = 1;
  100.  
  101.         /* make NSEG positive again */
  102.         NSEG = -NSEG;
  103.     }
  104.  
  105.    inf.ignore(120, '\n');           // ignore rest of line, up to 120 chars
  106.    inf >> X_MAX >> Y_MAX;
  107.    inf.ignore(120, '\n');
  108.    inf >> width;     // read the header section:
  109.    inf.ignore(120, '\n');
  110.    inf >> TRK_STRT_X >> TRK_STRT_Y;
  111.    inf.ignore(120, '\n');
  112.    inf >> SCORE_BOARD_X >> SCORE_BOARD_Y;
  113.    inf.ignore(120, '\n');
  114.    inf >> LDR_BRD_X >> LDR_BRD_Y;
  115.    inf.ignore(120, '\n');
  116.    inf >> IP_X >> IP_Y;
  117.    inf.ignore(120, '\n');
  118.    inf >> LOTIX >> LOTIY;
  119.    inf.ignore(120, '\n');
  120.    inf >> FINISH;
  121.    inf.ignore(120, '\n');
  122.  
  123.    trackout = new segment[NSEG];    // allocate RAM for track segment arrays
  124.    trackin = new segment[NSEG];
  125.  
  126.     /* centerline description? */
  127.     if (centerline == 1)
  128.     {
  129.         halfwidth = width / 2.0;
  130.         for (i = 0; i < NSEG; i++)
  131.         {
  132.             /* read the radius and length and ignore following comments */
  133.             inf >> radius >> length;
  134.             inf.ignore(120, '\n');
  135.  
  136.             /* check for degrees or radians */
  137.             if ((degrees == -1) && (radius != 0))
  138.             {
  139.                 /* it isn't pretty, but it works */
  140.                 if (length < 5.0)
  141.                 {
  142.                     degrees = 0;
  143.                 }
  144.                 else
  145.                 {
  146.                     degrees = 1;
  147.                 }
  148.             }
  149.  
  150.             /* convert degrees to radians, if necessary */
  151.             if ((degrees == 1) && (radius != 0.0))
  152.             {
  153.                 length /= DEGPRAD;
  154.             }
  155.  
  156.             /* fill in radius */
  157.             if (radius == 0.0)
  158.             {
  159.                 trackin[i].radius = 0.0;
  160.                 trackout[i].radius = 0.0;
  161.             }
  162.             else
  163.             {
  164.                 trackin[i].radius = radius - halfwidth;
  165.                 trackout[i].radius = radius + halfwidth;
  166.             }
  167.  
  168.             /* fill in length */
  169.             trackin[i].length = length;
  170.             trackout[i].length = length;
  171.         }
  172.     }
  173.     else
  174.     {
  175.         /* this is the original track file reader routine */
  176.  
  177.    for(i=0; i<NSEG; i++)  {         // read the segment data:
  178.       inf >> trackout[i].radius >> trackout[i].length;
  179.       inf.ignore(120, '\n');
  180.       // this section of the loop handles (degree vs. radians):
  181.       if(degrees == -1 && trackout[i].radius != 0.0)
  182.          if(trackout[i].length < 5.0)
  183.             degrees = 0;
  184.          else
  185.             degrees = 1;
  186.       if(degrees == 1 && trackout[i].radius != 0.0)
  187.          trackout[i].length /= DEGPRAD;
  188.    }
  189.  
  190.    for(i=0; i<NSEG; i++) {             // fill in trackin[] from trackout[]
  191.       if(trackout[i].radius == 0.0)
  192.          trackin[i].radius = trackout[i].radius;
  193.       else
  194.          trackin[i].radius = trackout[i].radius - width;
  195.       trackin[i].length = trackout[i].length;
  196.    }
  197.  
  198.         /* end of the original track file reader routine */
  199.     }
  200.  
  201.    // initialize this global variable:
  202.    from_start_to_seg1 = (1.0 - FINISH) * trackout[0].length;
  203. }
  204.  
  205. track_desc get_track_description(void)
  206. {
  207.    track_desc ts;
  208.  
  209.    ts.NSEG = NSEG;
  210.    ts.width = width;
  211.    ts.trackout = trackout;
  212.    ts.trackin = trackin;
  213.    return ts;
  214. }
  215.